
package xj.graph2d.app.prop;

import static xj.app.BaseApplicationFrame.getLookAndFeelManager;
import static xj.app.BaseApplicationFrame.getResourceString;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import xj.app.BaseApplicationFrame;
import xj.app.LookAndFeelManager;
import xj.graph2d.app.DrawAction;
import xj.graph2d.dialog.DialogPanel;
import xj.graph2d.dialog.TitledDialogPanel;

public class LookAndFeelListener 
  extends DrawAction
  implements ActionListener {

  class LookAndFeelOptionPanel extends DialogPanel {

    public LookAndFeelOptionPanel(String name) {
      super(name);
      JLabel lfLabel = new JLabel(getResourceString("LookAndFeel.text") + " ");
      lfField = new JComboBox();

      LookAndFeelManager lnfManager = BaseApplicationFrame.getLookAndFeelManager();
      List<LookAndFeelManager.LookAndFeel> all = lnfManager.getAvailableLookAndFeel();
      lnfs = new LookAndFeelManager.LookAndFeel[all.size()]; 
      lnfManager.getAvailableLookAndFeel().toArray(lnfs);
      for (int i = 0; i < lnfs.length; i++) { 
	lfField.addItem(lnfs[i].getDisplayName());
      }
      addRow(lfLabel, lfField);

      setPreferredSize(new Dimension(400, 150));
      setOption();
    }

    @Override
    public void setOption() {
      //lfField.setSelectedItem(lf);
    }

    @Override
    public void applyOption() {
      LookAndFeelManager lnfManager = getLookAndFeelManager();
      int i = lfField.getSelectedIndex();
      lnfManager.setLookAndFeel(lnfs[i]);

      /* 
      String lf = lfClassNames[i]; 
      if (lf == null) { 
	lf = DrawApp.getDefaultLookAndFeel();
      }
      DrawApp.setLookAndFeel(lf);
      */
    }

    protected JComboBox lfField;

  }
  
  LookAndFeelManager.LookAndFeel[] lnfs; 

  protected TitledDialogPanel lfOptionDialog = null;
  protected LookAndFeelOptionPanel lfPanel = null;
  
  protected String lfPanelTitle;
  protected String lfDialogTitle;
  protected String lfHeader;
  protected String[] lfText;
  
  @Override
  protected void init() { 
    lfPanelTitle = getResourceString("LookAndFeel.title");
    lfDialogTitle = getResourceString("LookAndFeelDialog.title");
    lfHeader = getResourceString("LookAndFeelDialog.header");
    lfText = new String[] { 
	getResourceString("LookAndFeelDialog.text1"),
	getResourceString("LookAndFeelDialog.text2"),	
    };
  }
  
  public void actionPerformed(ActionEvent e) {
    if (lfOptionDialog == null) {
      DialogPanel[] options = { 
	lfPanel = new LookAndFeelOptionPanel(lfPanelTitle), 
      };
      lfOptionDialog = new TitledDialogPanel(main.getJFrame(), 
					     JOptionPane.OK_CANCEL_OPTION, 
					     null, options);
      lfOptionDialog.setTitle(lfDialogTitle);
      lfOptionDialog.setTitlePaneText(lfHeader, lfText);
    }
    lfOptionDialog.setOptions();
    lfOptionDialog.showOptionDialog();
  }

}
